
export const useBasicStore = defineStore('basic', () => {

  /**
   * 配置筛选机制，用于处理查询操作时的筛选条件
   *
   * 键
   * - 阿姨列表 aunt-filter
   * - 客户线索 clue-filter
   *
   * 存储方式: map<'name-filter' | 'test-filter', FilterMapper>
   *
   */
  const filterMapper = ref<Map<string, Tool.Options>>(new Map());

  // key: ('name-filter' | 'test-filter') + ':' + filterKey
  const setFilterMapper = (key: string, value: Tool.Options) => {
    filterMapper.value.set(key, value);
  }

  // key: ('name-filter' | 'test-filter') + ':' + filterKey
  // 查询缓存, 如果缓存不存在, 则查询数据库, 并缓存结果
  const getFilterMapper = async (options: {
    key: string
    filterKey: string
    query: () => Promise<Tool.Options>
  }) => {
    const { key, filterKey, query } = options;
    const filter = filterMapper.value.has(`${key}:${filterKey}`);
    if (!filter) {
      const options = await query();
      filterMapper.value.set(`${key}:${filterKey}`, options);
      return options;
    }
    return filter;
  }

  return {
    ...{ setFilterMapper, getFilterMapper },
  };
})
